﻿using MikuMikuLibrary.Databases;
using MikuMikuLibrary.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseConverter
{
    class ObjMerge
    {
        public ObjectDatabase db = new ObjectDatabase();
        public ObjectDatabase db2 = new ObjectDatabase();
        public string logs = "";
        public void MergeAet(string filter = "", string whitelist = "")
        {
            int MaxId = 0;
            foreach (var i in db.Objects)
            {
                if (i.Id > MaxId)
                {
                    MaxId = i.Id;
                }
            }
           
            foreach (var i in db2.Objects)
            {
                //
                if ((i.Name.StartsWith("st", StringComparison.OrdinalIgnoreCase))
                    || (i.Name.StartsWith("eff", StringComparison.OrdinalIgnoreCase)))
                {
                    //db.Objects.RemoveAll(c => c.TextureFileName == i.TextureFileName);
                }

                var stringcheck = db.Objects.Where(c => c.TextureFileName == i.TextureFileName).FirstOrDefault();

                bool check = false;
                if (stringcheck == null)
                    check = true;
                else
                {
                    
                }

                if (filter != "")
                {
                    if ((i.Name.Contains(filter)))
                    {
                        check = false;
                    }
                    else
                    {

                    }
                }

                if (whitelist != "")
                {
                    if (i.Name.StartsWith(whitelist, StringComparison.OrdinalIgnoreCase))
                    {

                    }
                    else
                    {
                        check = false;
                    }
                }

                if ((i.Name.StartsWith("st", StringComparison.OrdinalIgnoreCase)) 
                    || (i.Name.StartsWith("eff", StringComparison.OrdinalIgnoreCase)))
                {

                } else
                {
                    //check = false;
                }

                if (check == true)
                {
                    var result = db.Objects.Where(c => c.Id == i.Id).FirstOrDefault();
                    if (result == null)
                    {
                        db.Objects.Add(i);
                    }
                    else
                    {
                        result = db.Objects.Where(c => c.Id == (ushort)(MaxId + 1)).FirstOrDefault();
                        while (result != null)
                        {
                            MaxId++;
                            result = db.Objects.Where(c => c.Id == (ushort)(MaxId + 1)).FirstOrDefault();
                        }
                        logs = logs + ("Warning: ID Changed from " + i.Id + " to " + (MaxId + 1)) + "\n";
                        i.Id = (ushort)(MaxId + 1);
                        db.Objects.Add(i);
                    }
                } else
                {
                    logs = logs + ("Warning : Skipped File " + i.FileName) + "\n";
                }

                
            }
        }
    }
}
